package com.nan.gbd.library.codec.recordcodec;

import android.media.MediaCodec;
import android.media.MediaMuxer;
import android.os.SystemClock;
import android.util.Log;

import com.nan.gbd.library.codec.MediaCodecConstant;
import com.nan.gbd.library.utils.AccountConfig;

import java.nio.ByteBuffer;


/**
 * 视频写入thread
 */
public class VideoCodecThread extends Thread {

    private static final String TAG = "VideoCodecThread.class";

    private MediaCodec videoCodec;
    private MediaCodec.BufferInfo bufferInfo;

    private boolean isStop;

    private long pts;
    private MediaMuxerChangeListener listener;


    public VideoCodecThread(MediaCodec mediaCodec, MediaCodec.BufferInfo bufferInfo, MediaMuxerChangeListener listener) {
        this.videoCodec = mediaCodec;
        this.bufferInfo = bufferInfo;
        this.listener = listener;
        pts = 0;
        MediaCodecConstant.videoTrackIndex = -1;
    }

    @Override
    public void run() {
        super.run();
        isStop = false;
        videoCodec.start();
        while (true) {
            if (isStop) {
                videoCodec.stop();
                videoCodec.release();
                videoCodec = null;
                MediaCodecConstant.videoStop = true;
                if (MediaCodecConstant.audioStop) {
                    listener.onMediaMuxerChangeListener(MediaCodecConstant.MUXER_STOP);
                    break;
                }
            }

            if (videoCodec == null)
                break;
            ByteBuffer[] outputBuffers = videoCodec.getOutputBuffers();
            int outputBufferIndex = videoCodec.dequeueOutputBuffer(bufferInfo, 1000);
            if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
                outputBuffers = videoCodec.getOutputBuffers();
            } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
                listener.onMediaFormatChange(true);
                if (MediaCodecConstant.audioTrackIndex != -1) {
                    listener.onMediaMuxerChangeListener(MediaCodecConstant.MUXER_START);
                }
            }
            while (outputBufferIndex >= 0) {
                if (!MediaCodecConstant.encodeStart) {
                    Log.d(TAG, "run: 线程延迟");
                    SystemClock.sleep(10);
                    continue;
                }

                ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
                outputBuffer.position(bufferInfo.offset);
                outputBuffer.limit(bufferInfo.offset + bufferInfo.size);
                if (pts == 0) {
                    pts = bufferInfo.presentationTimeUs;
                }
                bufferInfo.presentationTimeUs = bufferInfo.presentationTimeUs - pts;
                if (bufferInfo.size != 0 && listener != null) {
                    listener.onEncodeOutput(MediaCodecConstant.videoTrackIndex, outputBuffer, bufferInfo);
//                    Log.d(TAG, "视频秒数时间戳 = " + bufferInfo.presentationTimeUs / 1000.0f);
//                    float time = (bufferInfo.presentationTimeUs / 1000.0f);
//                    listener.onMediaInfoListener((int) time);
                }

                videoCodec.releaseOutputBuffer(outputBufferIndex, false);
                outputBufferIndex = videoCodec.dequeueOutputBuffer(bufferInfo, 1000);
            }
        }
    }

    public void stopVideoCodec() {
        isStop = true;
    }

    public boolean isStop() {
        return isStop;
    }
}
